package com.natasa.monitor.service;

import org.springframework.beans.factory.annotation.Autowired;

import com.natasa.monitor.dao.IMonitorResultDAO;
import com.natasa.monitor.dao.IMonitorRuleDAO;
import com.natasa.monitor.entity.monitor.BaseMonitorRule;
import com.natasa.monitor.entity.monitor.MonitorResult;

public abstract class AbstractMonitorTask {
	protected BaseMonitorRule moninorRule;
	
	protected MonitorResult motinorResult;
	
	protected String monitorRuleId;
	
	@Autowired
	protected IMonitorResultDAO monitorResultDAO;
	
	@Autowired
	protected IMonitorRuleDAO monitorRuleDAO;
	
	@Autowired
	protected ParserFactory parserFactory;

	private void before() {
		moninorRule = monitorRuleDAO.getMonitorRuleById(monitorRuleId);
	}

	protected abstract String doMonitor();

	private void after() {
		monitorResultDAO.saveMonitorResult(motinorResult);
	}
	
	public void doTask(){
		before();
		String result = doMonitor();
		IParser parser = (IParser) parserFactory.getParser(moninorRule.getDataParser());
		motinorResult = parser.parser(result);
		after();
	}

	public void setMonitorRuleId(String monitorRuleId) {
		this.monitorRuleId = monitorRuleId;
	}
}
